package com.travel.goods.service.impl;

import com.travel.common.exception.LogicException;
import com.travel.goods.dao.GoodsDao;
import com.travel.goods.entity.Goods;
import com.travel.goods.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * @classDesc: 功能描述:()
 * @author:<a href="leeue@foxmail.com">李月</a>
 * @Version:v1.0
 * @createTime:2019年3月19日 下午4:44:41
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	GoodsDao goodsDao;

	@Override

	public Goods addGoods(Goods goods) {
		return goodsDao.save(goods);
	}

	@Override
	public void deleteGoods(Goods goods) {
		goodsDao.delete(goods);

	}

	@Override
	public void editGoods(Goods goods) {
		goodsDao.saveAndFlush(goods);

	}

	@Override
	public List<Goods> findByGoodsName(String goodsName) {
		if (!StringUtils.isEmpty(goodsName)) {
			throw  new LogicException("输入商品为空",500);
			// return baseApiService.setResultError("输入商品名为空");
		}
		return goodsDao.findByGoodsName(goodsName);

	}

	@Override
	public List<Goods> findByGoodsCity(String goodCity) {

		return goodsDao.findByGoodsCity(goodCity);
	}

	@Override
	public Optional<Goods> findByGoodsId(Long id) {
		
		return goodsDao.findById(id);
	}

}
